local love = require 'love'
local fontHandler = require('assets-handler.font-handler')
local bounceObject = require('utils.bounce-object')

local setColor = love.graphics.setColor
local printg = love.graphics.print

local width = Width
local height = Height

local bounceText = {
    text = "任意键返回",
    textX = 0,
    textY = 0,
    textWidth = 0,
    textHeight = 0,
    fontSize = 12,
    color = {1, 1, 1, 1},
    showTimes = 2, -- 最大激活次数
    duration = 6, -- 每次的最大持续时间
    updateTimer = 0,
    isActive = false
}

local function init()
    fontHandler.setFont(bounceText.fontSize)
    bounceText.textWidth = fontHandler.getFontWidth(bounceText.text)
    bounceText.textHeight = fontHandler.getFontHeight()

    bounceText.textX = (width - bounceText.textWidth) / 2
    bounceText.textY = (height - bounceText.textHeight) / 2

    bounceObject.init(bounceText.textX, bounceText.textY, bounceText.textWidth, bounceText.textHeight)
end

local function update(dt)
    if bounceText.isActive then
        bounceText.updateTimer = bounceText.updateTimer + dt
        if bounceText.updateTimer >= bounceText.duration then
            bounceText.isActive = false
            bounceText.updateTimer = 0
        end

        bounceObject.update(dt)
    end
end

local function drawBounceText()
    if bounceText.isActive then
        fontHandler.setFont(bounceText.fontSize)
        setColor(bounceText.color)
        printg(bounceText.text, bounceObject.getX(), bounceObject.getY())
    end
end

local function active()
    if bounceText.showTimes > 0 then
        bounceText.isActive = true
        bounceText.showTimes = bounceText.showTimes - 1
    end
end

return {
    init = init,
    update = update,
    drawBounceText = drawBounceText,
    active = active,
    deactive = function() bounceText.isActive = false end
}